<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="课程名称" prop="courseName">
        <el-input v-model="formData.courseName" placeholder="请输入课程名称" />
      </el-form-item>
      <el-form-item label="开课班级" prop="courseClass">
        <el-input v-model="formData.courseClass" placeholder="请输入开课班级" />
      </el-form-item>
      <el-form-item label="班级数" prop="classSize">
        <el-input v-model="formData.classSize" placeholder="请输入班级数" />
      </el-form-item>
      <el-form-item label="学生人数" prop="studentSize">
        <el-input v-model="formData.studentSize" placeholder="请输入学生人数" />
      </el-form-item>
      <el-form-item label="理论学时" prop="theoryAmount">
        <el-input v-model="formData.theoryAmount" placeholder="请输入理论学时" />
      </el-form-item>
      <el-form-item label="课程系数" prop="theoryCourseCoefficient">
        <el-input v-model="formData.theoryCourseCoefficient" placeholder="请输入课程系数" />
      </el-form-item>
      <el-form-item label="合班系数" prop="theoryCombinedCoefficient">
        <el-input v-model="formData.theoryCombinedCoefficient" placeholder="请输入合班系数" />
      </el-form-item>
      <el-form-item label="难度系数" prop="difficultyCoefficient">
        <el-input v-model="formData.difficultyCoefficient" placeholder="请输入难度系数" />
      </el-form-item>
      <el-form-item label="实验学时" prop="experimentAmount">
        <el-input v-model="formData.experimentAmount" placeholder="请输入实验学时" />
      </el-form-item>
      <el-form-item label="课程系数" prop="experimentCourseCoefficient">
        <el-input v-model="formData.experimentCourseCoefficient" placeholder="请输入课程系数" />
      </el-form-item>
      <el-form-item label="合班系数" prop="experimentCombinedCoefficient">
        <el-input v-model="formData.experimentCombinedCoefficient" placeholder="请输入合班系数" />
      </el-form-item>
      <el-form-item label="折合学时" prop="equivalentAmount">
        <el-input v-model="formData.equivalentAmount" placeholder="请输入折合学时" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { TeacherTeachApi } from '@/api/data/teacherteach'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  dataTeacherTeachId: undefined,
  courseName: undefined,
  courseClass: undefined,
  classSize: undefined,
  studentSize: undefined,
  theoryAmount: undefined,
  theoryCourseCoefficient: undefined,
  theoryCombinedCoefficient: undefined,
  difficultyCoefficient: undefined,
  experimentAmount: undefined,
  experimentCourseCoefficient: undefined,
  experimentCombinedCoefficient: undefined,
  equivalentAmount: undefined,
  status: undefined
})
const formRules = reactive({
  dataTeacherTeachId: [{ required: true, message: '教学id不能为空', trigger: 'blur' }],
  courseName: [{ required: true, message: '课程名称不能为空', trigger: 'blur' }],
  courseClass: [{ required: true, message: '开课班级不能为空', trigger: 'blur' }],
  classSize: [{ required: true, message: '班级数不能为空', trigger: 'blur' }],
  studentSize: [{ required: true, message: '学生人数不能为空', trigger: 'blur' }],
  theoryAmount: [{ required: true, message: '理论学时不能为空', trigger: 'blur' }],
  theoryCourseCoefficient: [{ required: true, message: '课程系数不能为空', trigger: 'blur' }],
  theoryCombinedCoefficient: [{ required: true, message: '合班系数不能为空', trigger: 'blur' }],
  difficultyCoefficient: [{ required: true, message: '难度系数不能为空', trigger: 'blur' }],
  experimentAmount: [{ required: true, message: '实验学时不能为空', trigger: 'blur' }],
  experimentCourseCoefficient: [{ required: true, message: '课程系数不能为空', trigger: 'blur' }],
  experimentCombinedCoefficient: [{ required: true, message: '合班系数不能为空', trigger: 'blur' }],
  equivalentAmount: [{ required: true, message: '折合学时不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, dataTeacherTeachId: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.dataTeacherTeachId = dataTeacherTeachId
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await TeacherTeachApi.getTeacherTeachTask(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value
    if (formType.value === 'create') {
      await TeacherTeachApi.createTeacherTeachTask(data)
      message.success(t('common.createSuccess'))
    } else {
      await TeacherTeachApi.updateTeacherTeachTask(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    dataTeacherTeachId: undefined,
    courseName: undefined,
    courseClass: undefined,
    classSize: undefined,
    studentSize: undefined,
    theoryAmount: undefined,
    theoryCourseCoefficient: undefined,
    theoryCombinedCoefficient: undefined,
    difficultyCoefficient: undefined,
    experimentAmount: undefined,
    experimentCourseCoefficient: undefined,
    experimentCombinedCoefficient: undefined,
    equivalentAmount: undefined,
    status: undefined
  }
  formRef.value?.resetFields()
}
</script>
